package com.xianyun.book.command.query.strategy;

import com.xianyun.book.common.core.handle.HandleParam;
import com.xianyun.book.common.core.handle.MultipleHandle;
import com.xianyun.book.common.core.handle.MultipleInvoker;
import com.xianyun.book.convertor.BookConvertor;
import com.xianyun.book.dto.clientobject.BookInfoCO;
import com.xianyun.book.gatewayimpl.elasticsearch.dataobject.BookEsDO;
import com.xianyun.book.gatewayimpl.elasticsearch.repository.BookEsRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;

/**
 * @// TODO: 数据源修改后续扩展时修改为保存后的数据索引 该索引后续负责ISBN查询 2021/5/30
 * @author macos-zyj
 */
@Slf4j
@Component
public class BookEsMultipleHandle implements MultipleHandle<BookInfoCO,String> {
    @Resource
    private BookEsRepository bookEsRepository;
    @Override
    public List<BookInfoCO> doListQuery(HandleParam<String> handleParam, MultipleInvoker<BookInfoCO, String> next) {
        List<BookEsDO> resultList = bookEsRepository.queryMatchList("title",handleParam.getHandleData());
        if (null != resultList && resultList.size()>0){
            return BookConvertor.toDataShowList(resultList);
        }
        resultList = bookEsRepository.queryMatchList("author",handleParam.getHandleData());
        if (null != resultList && resultList.size()>0){
            return BookConvertor.toDataShowList(resultList);
        }
        resultList = bookEsRepository.queryMatchList("publish",handleParam.getHandleData());
        if (null != resultList && resultList.size()>0){
            return BookConvertor.toDataShowList(resultList);
        }
        return next.invokeList(handleParam);
    }
}
